﻿using CommunityToolkit.Mvvm.Messaging;
using LongSCADA_WPF.Message;
using LongSCADA_WPF.Services;
using LongSCADA_WPF.ViewModels;
using MahApps.Metro.Controls;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace LongSCADA_WPF.Views
{
    /// <summary>
    /// ShellView.xaml 的交互逻辑
    /// </summary>
    public partial class ShellView : MetroWindow
    {
        private UserModelService _userModelService;
        public ShellView(UserModelService userModelService)
        {
            InitializeComponent();
            InitData();
            InitChangeLoginAndWindowView();
            _userModelService = userModelService;
        }

        private void InitChangeLoginAndWindowView()
        {
            //绑定登录视图
            container.Content = App.Current.Services.GetService<LoginView>();
            //若登录成功，则回到主界面(来自于 LoginViewModel.cs 的消息注册)
            WeakReferenceMessenger.Default.Register<LoginMessage>(this, (sender, arg) => 
            {
                // Register<LoginMessage> -- 注册消息要正确
                //实现登录跳转
                container.Content = App.Current.Services.GetService<MainView>();
                Width = 1200;
                Height = 600;
                Title = "LongSCADA";
                //设置窗体弹出坐标
                SetWindowLocation();
            });

            //登出，回到 Login 界面(来自于 MainViewModel.cs 的消息注册)
            WeakReferenceMessenger.Default.Register<LogOutMessage>(this, (sender, arg) => 
            {
                _userModelService.CurrentUserModel = new Models.UserModel()
                {
                    UserName = "visitor",
                    PassWord = "1111"
                };
                // Register<LoginMessage> -- 注册消息要正确
                //实现登录跳转
                container.Content = App.Current.Services.GetService<LoginView>();
                Width = 800;
                Height = 450;
                //设置窗体弹出坐标
                SetWindowLocation();
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void SetWindowLocation()
        {
            Left = (SystemParameters.WorkArea.Width - Width) / 3;
            Top = (SystemParameters.WorkArea.Height - Height) / 3;
        }

        /// <summary>
        /// 绑定 ViewModel 数据
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void InitData()
        {
            //通过依赖注入进行绑定
            DataContext = App.Current.Services.GetService<ShellViewModel>();
        }
    }
}
